package samples.d2.lines 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.Shape;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.setInterval;
	import samples.BaseSample;
	import shapes.shapes2D.distribution.RandomDistribution;
	import utils.GeomUtils;
	
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class Lines9 extends BaseSample
	{
		private var canvas:Shape = new Shape();
		private var p0:Point;
		private var p1:Point;
		private var points:Vector.<Point>;
		private var mouseDown:Boolean;
		private var bd:BitmapData;
		private var lx:Number;
		private var ly:Number;
		private var distMin:Number;
		
		private var definition:Vector.<Number> = Vector.<Number>( [106.95,-103.35,109.5,-103.35,112.05,-103.35,112.05,-103.35,112.05,-85.45,112.05,-67.6,112.05,-67.6,112.05,-52.05,103.3,-43.9,103.3,-43.9,95.9,-37,84.55,-37,84.55,-37,72.85,-37,65.35,-44.4,65.35,-44.4,57.2,-52.4,57.2,-66.5,57.2,-66.5,57.2,-84.9,57.2,-103.35,57.2,-103.35,59.75,-103.35,62.3,-103.35,62.3,-103.35,62.3,-85,62.3,-66.7,62.3,-66.7,62.3,-54.45,69,-47.75,69,-47.75,75.05,-41.7,84.55,-41.7,84.55,-41.7,93.9,-41.7,99.95,-47.55,99.95,-47.55,106.95,-54.35,106.95,-67.35,106.95,-67.35,106.95,-85.35,106.95,-103.35,2,-81.8,9.65,-81.8,15.05,-76.85,15.05,-76.85,21.1,-71.1,21.1,-61,21.1,-61,4.1,-61,-12.95,-61,-12.95,-61,-13,-59.85,-13,-58.8,-13,-58.8,-13,-51.5,-7.85,-46.5,-7.85,-46.5,-2.7,-41.5,4.25,-41.5,4.25,-41.5,12.35,-41.5,20.3,-47.95,20.3,-47.95,20.3,-45.35,20.3,-42.75,20.3,-42.75,12.8,-37,3.7,-37,3.7,-37,-5.7,-37,-11.75,-43.45,-11.75,-43.45,-17.8,-49.95,-17.8,-59.95,-17.8,-59.95,-17.8,-69.45,-12.2,-75.65,-12.2,-75.65,-6.55,-81.8,2,-81.8,15.6,-64.75,13.25,-77.3,2,-77.3,-12.3,-64.75,1.65,-64.75,15.6,-64.75,-7.2,-73.95,-11.05,-70.6,-12.3,-64.75,2,-77.3,-3.4,-77.3,-7.2,-73.95,126.3,-78.4,130.35,-81.8,136.2,-81.8,136.2,-81.8,141.4,-81.8,147.7,-78.15,147.7,-78.15,147.7,-75.65,147.7,-73.2,147.7,-73.2,140.95,-77.6,135.9,-77.6,135.9,-77.6,131.95,-77.6,129.3,-75.5,129.3,-75.5,126.7,-73.4,126.7,-70.3,126.7,-70.3,126.7,-66.05,132.45,-63.5,132.45,-63.5,137.8,-61.2,143.25,-58.8,143.25,-58.8,149.05,-55.25,149.05,-48.9,149.05,-48.9,149.05,-43.8,144.95,-40.45,144.95,-40.45,140.9,-37,134.95,-37,134.95,-37,128.4,-37,122,-40.95,122,-40.95,122,-43.2,122,-45.45,122,-45.45,129.75,-41.5,135.55,-41.5,135.55,-41.5,139.45,-41.5,142.05,-43.6,142.05,-43.6,144.65,-45.7,144.65,-49,144.65,-49,144.65,-53.2,138.85,-55.9,138.85,-55.9,133.5,-58.3,128.05,-60.7,128.05,-60.7,122.25,-64.15,122.25,-70,122.25,-70,122.25,-75,126.3,-78.4,155.9,-59.95,155.9,-69.45,161.5,-75.65,161.5,-75.65,167.15,-81.8,175.7,-81.8,175.7,-81.8,183.35,-81.8,188.75,-76.85,188.75,-76.85,194.8,-71.1,194.8,-61,194.8,-61,177.8,-61,160.75,-61,160.75,-61,160.7,-59.85,160.7,-58.8,160.7,-58.8,160.7,-51.5,165.85,-46.5,165.85,-46.5,171,-41.5,177.95,-41.5,177.95,-41.5,186.05,-41.5,194,-47.95,194,-47.95,194,-45.35,194,-42.75,194,-42.75,186.5,-37,177.4,-37,177.4,-37,168,-37,161.95,-43.45,161.95,-43.45,155.9,-49.95,155.9,-59.95,175.7,-77.3,170.3,-77.3,166.5,-73.95,189.3,-64.75,186.95,-77.3,175.7,-77.3,161.4,-64.75,175.35,-64.75,189.3,-64.75,166.5,-73.95,162.65,-70.6,161.4,-64.75,223.95,-81.8,228.1,-81.8,233.85,-76.7,233.85,-76.7,232.55,-74.55,231.2,-72.4,231.2,-72.4,225.8,-77.3,222.5,-77.3,222.5,-77.3,216,-77.3,208.7,-64.95,208.7,-64.95,208.7,-51.4,208.7,-37.85,208.7,-37.85,206.4,-37.85,204.1,-37.85,204.1,-37.85,204.1,-59.4,204.1,-80.95,204.1,-80.95,206.4,-80.95,208.7,-80.95,208.7,-80.95,208.7,-75.3,208.7,-69.65,208.7,-69.65,208.8,-69.65,208.9,-69.65,208.9,-69.65,216.55,-81.8,223.95,-81.8,190.6,68.9,190.6,71,190.6,73.1,190.6,73.1,171.9,73.1,153.15,73.1,153.15,73.1,153.15,40.35,153.15,7.6,153.15,7.6,155.7,7.6,158.25,7.6,158.25,7.6,158.25,38.25,158.25,68.9,158.25,68.9,174.45,68.9,190.6,68.9,137.4,7.6,140.65,7.6,143.9,7.6,143.9,7.6,129.85,23.6,115.8,39.6,115.8,39.6,130.7,56.35,145.55,73.1,145.55,73.1,142.3,73.1,139.05,73.1,139.05,73.1,125.8,58.15,112.55,43.15,112.55,43.15,99.3,58.15,86,73.1,86,73.1,82.75,73.1,79.5,73.1,79.5,73.1,94.35,56.35,109.15,39.6,109.15,39.6,95.1,23.6,81.05,7.6,81.05,7.6,84.3,7.6,87.55,7.6,87.55,7.6,100.05,21.75,112.55,35.85,112.55,35.85,125,21.75,137.4,7.6,14.5,30,14.5,31.2,14.5,32.4,14.5,32.4,21.1,29.15,27.5,29.15,27.5,29.15,36.7,29.15,42.75,35.45,42.75,35.45,48.8,41.8,48.8,51.4,48.8,51.4,48.8,60.9,42.55,67.45,42.55,67.45,36.3,73.95,27.3,73.95,27.3,73.95,21.1,73.95,14.5,70.7,14.5,70.7,14.5,79.4,14.5,88.05,14.5,88.05,12.2,88.05,9.9,88.05,9.9,88.05,9.9,59.05,9.9,30,9.9,30,12.2,30,14.5,30,38.65,38.8,33.6,33.65,25.85,33.65,43.7,51.75,43.7,43.9,38.65,38.8,38.95,64.6,43.7,59.75,43.7,51.75,26.4,69.45,34.2,69.45,38.95,64.6,14.5,66.15,20.55,69.45,26.4,69.45,14.5,36.8,14.5,51.5,14.5,66.15,25.85,33.65,20.45,33.65,14.5,36.8,-205.05,-103.8,-203.65,-103.8,-202.25,-103.8,-202.25,-103.8,-187.1,-70.8,-172,-37.85,-172,-37.85,-174.65,-37.85,-177.3,-37.85,-177.3,-37.85,-182.45,-49.15,-187.65,-60.5,-187.65,-60.5,-203.6,-60.5,-219.6,-60.5,-219.6,-60.5,-224.8,-49.15,-230,-37.85,-230,-37.85,-232.6,-37.85,-235.25,-37.85,-235.25,-37.85,-220.15,-70.8,-205.05,-103.8,-203.7,-95.55,-210.7,-80.1,-217.7,-64.7,-189.55,-64.7,-196.6,-80.1,-203.7,-95.55,-217.7,-64.7,-203.6,-64.7,-189.55,-64.7,-64.65,-103.35,-62.35,-103.35,-60.05,-103.35,-60.05,-103.35,-60.05,-89.05,-60.05,-74.8,-60.05,-74.8,-52.85,-81.8,-44.5,-81.8,-44.5,-81.8,-36.05,-81.8,-30.7,-75.95,-30.7,-75.95,-25.3,-70.1,-25.3,-60.8,-25.3,-60.8,-25.3,-49.95,-31.9,-43.45,-31.9,-43.45,-38.45,-37,-49.4,-37,-49.4,-37,-56.4,-37,-64.65,-40.3,-64.65,-40.3,-64.65,-71.8,-64.65,-103.35,-30.2,-60.5,-30.2,-68.1,-34.35,-73.05,-35.65,-46.9,-30.2,-52.25,-30.2,-60.5,-49.5,-41.5,-41,-41.5,-35.65,-46.9,-60.05,-43.5,-54.65,-41.5,-49.5,-41.5,-60.05,-69.65,-60.05,-56.55,-60.05,-43.5,-45.05,-77.95,-52.65,-77.95,-60.05,-69.65,-34.35,-73.05,-38.55,-77.95,-45.05,-77.95,-133.05,-103.35,-130.75,-103.35,-128.45,-103.35,-128.45,-103.35,-128.45,-70.6,-128.45,-37.85,-128.45,-37.85,-130.75,-37.85,-133.05,-37.85,-133.05,-37.85,-133.05,-39.5,-133.05,-41.2,-133.05,-41.2,-139.2,-37,-145.8,-37,-145.8,-37,-155.05,-37,-161.35,-43.45,-161.35,-43.45,-167.6,-49.95,-167.6,-59.45,-167.6,-59.45,-167.6,-69.15,-161.45,-75.5,-161.45,-75.5,-155.25,-81.8,-145.8,-81.8,-145.8,-81.8,-140.05,-81.8,-133.05,-78.35,-133.05,-78.35,-133.05,-90.85,-133.05,-103.35,-133.05,-73.3,-139.4,-77.3,-145.65,-77.3,-133.05,-45.45,-133.05,-59.35,-133.05,-73.3,-144.7,-41.5,-138.95,-41.5,-133.05,-45.45,-157.6,-46.7,-152.45,-41.5,-144.7,-41.5,-162.75,-59.75,-162.75,-51.85,-157.6,-46.7,-157.95,-72.4,-162.75,-67.45,-162.75,-59.75,-145.65,-77.3,-153.15,-77.3,-157.95,-72.4,-112.3,-75.4,-106,-81.8,-96.6,-81.8,-96.6,-81.8,-87.2,-81.8,-81,-75.45,-81,-75.45,-74.75,-69.05,-74.75,-59.35,-74.75,-59.35,-74.75,-49.75,-81,-43.4,-81,-43.4,-87.2,-37,-96.6,-37,-96.6,-37,-106,-37,-112.3,-43.4,-112.3,-43.4,-118.55,-49.75,-118.55,-59.35,-118.55,-59.35,-118.55,-68.95,-112.3,-75.4,-108.85,-72.2,-113.7,-67.15,-113.7,-59.35,-96.6,-77.3,-104,-77.3,-108.85,-72.2,-84.45,-72.15,-89.3,-77.3,-96.6,-77.3,-79.6,-59.35,-79.6,-67.05,-84.45,-72.15,-84.45,-46.6,-79.6,-51.7,-79.6,-59.35,-96.6,-41.5,-89.3,-41.5,-84.45,-46.6,-108.85,-46.6,-104,-41.5,-96.6,-41.5,-113.7,-59.35,-113.7,-51.7,-108.85,-46.6,-37.2,30,-34.9,30,-32.6,30,-32.6,30,-32.6,43.3,-32.6,56.55,-32.6,56.55,-32.6,62.85,-29.05,66.25,-29.05,66.25,-25.9,69.45,-20.8,69.45,-20.8,69.45,-16,69.45,-12.65,67.05,-12.65,67.05,-9.85,65.05,-6.8,60.2,-6.8,60.2,-6.8,45.1,-6.8,30,-6.8,30,-4.5,30,-2.2,30,-2.2,30,-2.2,51.55,-2.2,73.1,-2.2,73.1,-4.5,73.1,-6.8,73.1,-6.8,73.1,-6.8,69.55,-6.8,66,-6.8,66,-9.85,70.3,-12.85,72,-12.85,72,-16.15,73.95,-21.05,73.95,-21.05,73.95,-27.55,73.95,-31.9,70.1,-31.9,70.1,-37.2,65.5,-37.2,56.65,-37.2,56.65,-37.2,43.35,-37.2,30,-69.2,29.15,-59.8,29.15,-53.6,35.5,-53.6,35.5,-47.35,41.9,-47.35,51.6,-47.35,51.6,-47.35,61.2,-53.6,67.55,-53.6,67.55,-59.8,73.95,-69.2,73.95,-69.2,73.95,-78.6,73.95,-84.9,67.55,-84.9,67.55,-91.15,61.2,-91.15,51.6,-91.15,51.6,-91.15,42,-84.9,35.55,-84.9,35.55,-78.6,29.15,-69.2,29.15,-57.05,38.8,-61.9,33.65,-69.2,33.65,-52.2,51.6,-52.2,43.9,-57.05,38.8,-57.05,64.35,-52.2,59.25,-52.2,51.6,-69.2,69.45,-61.9,69.45,-57.05,64.35,-81.45,64.35,-76.6,69.45,-69.2,69.45,-86.3,51.6,-86.3,59.25,-81.45,64.35,-81.45,38.75,-86.3,43.8,-86.3,51.6,-69.2,33.65,-76.6,33.65,-81.45,38.75,-116.55,41.3,-116.45,41.3,-116.35,41.3,-116.35,41.3,-108.7,29.15,-101.3,29.15,-101.3,29.15,-97.15,29.15,-91.4,34.25,-91.4,34.25,-92.7,36.4,-94.05,38.55,-94.05,38.55,-99.45,33.65,-102.75,33.65,-102.75,33.65,-109.25,33.65,-116.55,46,-116.55,46,-116.55,59.55,-116.55,73.1,-116.55,73.1,-118.85,73.1,-121.15,73.1,-121.15,73.1,-121.15,51.55,-121.15,30,-121.15,30,-118.85,30,-116.55,30,-116.55,30,-116.55,35.65,-116.55,41.3,-156.2,6.75,-146.15,6.75,-133.75,12.35,-133.75,12.35,-133.75,15.25,-133.75,18.1,-133.75,18.1,-145.5,11.45,-157,11.45,-157,11.45,-169.5,11.45,-177.7,19.65,-177.7,19.65,-177.9,19.85,-178.1,20.05,-178.1,20.05,-185.9,28.15,-185.9,40.4,-185.9,40.4,-185.9,52.4,-178.5,60.35,-178.5,60.35,-170.15,69.25,-155.55,69.25,-155.55,69.25,-145.65,69.25,-137.6,65.25,-137.6,65.25,-137.6,56.4,-137.6,47.55,-137.6,47.55,-144.5,47.55,-151.45,47.55,-151.45,47.55,-151.45,45.45,-151.45,43.35,-151.45,43.35,-141.95,43.35,-132.5,43.35,-132.5,43.35,-132.5,55.85,-132.5,68.35,-132.5,68.35,-144.05,73.95,-155.75,73.95,-155.75,73.95,-171.05,73.95,-181.2,64.3,-181.2,64.3,-191.35,54.7,-191.35,40.15,-191.35,40.15,-191.35,28.45,-184.8,20.05,-184.8,20.05,-183.3,18.1,-181.4,16.3,-181.4,16.3,-171.4,6.75,-156.2,6.75] );
		private var index:int = 0;
		private var scale:Number = 2;

		public function Lines9() 
		{
			_stage.addEventListener( MouseEvent.MOUSE_DOWN, mouseHandler );
			_stage.addEventListener( MouseEvent.MOUSE_UP, mouseHandler );
			
			points = RandomDistribution.distribute( new Rectangle( 0,0,sw, sh ), 1000 )
			//new Vector.<Point>();
			
			bd = new BitmapData( sw, sh, true, 0 );
			addChild( new Bitmap( bd ) );
			
			
			distMin = 50*50;
			
			interval = setInterval( update, 20 );
			
			
		}
		
		private function mouseHandler(e:MouseEvent):void 
		{
			switch( e.type )
			{
				case MouseEvent.MOUSE_DOWN:
				mouseDown = true;
				lx = _stage.mouseX;
				ly = _stage.mouseY;
				break;
				
				case MouseEvent.MOUSE_UP:
				mouseDown = false;
				break;
			}
		}
		
		private function update( e:MouseEvent = null ):void 
		{
			
			canvas.graphics.clear();
			canvas.graphics.lineStyle( 3, color , .5 );
			//canvas.graphics.beginFill( color , 1 );
			
			index %= definition.length;
			
			var i:int = index;
			var offset:Point = new Point( sw / 2, sh / 2 );
			
			p0 = new Point( offset.x + definition[i] * scale, 
							offset.y + definition[i + 1] * scale);
								
			canvas.graphics.moveTo( p0.x, p0.y );
			
			canvas.graphics.curveTo( 	offset.x + definition[i + 2] * scale,
										offset.y + definition[i + 3] * scale,  
										offset.x + definition[i + 4] * scale, 
										offset.y + definition[i + 5] * scale	);
			index += 6;
			bd.draw( canvas, null, null, BlendMode.ADD );
			//bd.colorTransform( bd.rect, new ColorTransform( 1, 1, 1, .99 ) );
			//bd.scroll( int( ( Math.random() - .5 )*2 )*2, int( ( Math.random() - .5 )*2 )*2 );
			//return
			
			
			canvas.graphics.clear();
			if ( mouseDown )
			{
				
				scale = 2 + ( Math.random() - .5 )*.2;
				//p0 = new Point( _stage.mouseX, _stage.mouseY ) 
				points.push( p0 );
			
				var dist:Number;
				
				for ( i = 0; i < points.length; i++ )
				{
					p1 = points[ i ];
					
					if ( Math.random() > .5 )
					{
						dist = GeomUtils.squareDistance( p0.x, p0.y, p1.x, p1.y );
						
						if( dist < distMin )
						{
							canvas.graphics.lineStyle( 0, color ,1 - dist / distMin );
							canvas.graphics.moveTo( p0.x, p0.y );
							canvas.graphics.lineTo( p1.x, p1.y );
						}
					}
					
				}
				
				canvas.graphics.lineStyle( 0, color , 1 );
				canvas.graphics.moveTo( _stage.mouseX, _stage.mouseY );
				canvas.graphics.lineTo( lx, ly );
				bd.draw( canvas );
			}
			else 
			{
				scale = 2;
			}
			
			
			lx = _stage.mouseX;
			ly = _stage.mouseY;
		
			
		}
		
	}

}